<?php
// .-----------------------------------------------------------------------------------
// | WE TRY THE BEST WAY 杭州博也网络科技有限公司
// |-----------------------------------------------------------------------------------
// | Author: 青 <99701759@qq.com>
// | Copyright (c) 2013-2016, http://www.itboye.com. All Rights Reserved.
// |-----------------------------------------------------------------------------------

namespace Home\Controller;
use Think\Controller;
use Weixin\Api\WxuserApi;
use Shop\Api\AddressApi;
use Shop\Api\ProductApi;
use Shop\Api\ProductSkuApi;
use Shop\Api\ProductImageApi;
use Admin\Api\UserPictureApi;
use Shop\Api\CategoryApi;
class AddressController extends HomeController {

    /*
     * 新增地址
     * */
    public function add_addre(){
        if(IS_POST){
            $map = array('openid' => $this -> openid, 'wxaccount_id' => $this -> wxaccount['id']);//wxaccount_id表示所属的微信公众账号
            $result = apiCall(WxuserApi::GET_INFO , array($map));
            $_POST['uid'] = $result['info']['id'];
            $address = explode(' ',$_POST['address']);
            $_POST['province'] = $address['0'];
            $_POST['city'] = $address['1'];
            $_POST['area'] = $address['2'];
            $_POST['address'] = null;
            $res = apiCall(AddressApi::ADD);
            if($res['status']){
                if(!empty(I('product_id'))){

                    $this->success('添加成功',U('User/buy_good',array('id'=> I('product_id'),'addressid' => $res['info'])));
                }else{
                    $this->redirect('UserCenter/addre_manage');
                }
            }else{
                $this->error('添加失败');
            }
        }else{
            if(!empty(I('product_id'))){
                $this->assign('product_id',I('product_id'));
            }
            $this->display();
        }
    }

    /*
     * 地址删除
     * */
    public function addre_delete(){
        $map = array('id' => I('get.id'));
        $res = apiCall(AddressApi::DELETE , array($map));
        if($res['status']){
            echo(true);
        }else{
            echo(false);
        }
    }
    /*
     * 地址编辑
     * */
    public  function addre_edit(){
        if(IS_POST){
            $map = array('id' => $_POST['id']);
            $address = explode(' ',$_POST['address']);
            $_POST['province'] = $address['0'];
            $_POST['city'] = $address['1'];
            $_POST['area'] = $address['2'];
            $_POST['address'] = null;
            $res = apiCall(AddressApi::SAVE , array($map, $_POST));
            if($res['status']){
                $this->redirect('UserCenter/addre_manage');
            }else{
                $this->error('修改失败');
            }
        }else{
            $map = array('id'=>I('get.id'));
            $addre = apiCall(AddressApi::GET_INFO , array($map));
            if($addre['status']){
                $this->assign($addre['info']);
            }
            $this->display();
        }

    }
    /*
     * 设置默认地址AJAX
     * */
    public function set_addre(){
        if(IS_GET){
            $map = array('openid' => $this -> openid, 'wxaccount_id' => $this -> wxaccount['id']);//wxaccount_id表示所属的微信公众账号
            $default = array('default_address' => I('get.id') );
            $res = apiCall(WxuserApi::SET_INC , array($map, $default));
            if($res['status']){
                echo(true);
            }else{
                echo(false);
            }
        }
    }

    /*
     * 选择收货地址
     * */
    public function select_address(){
        $map = array('openid' => $this -> openid, 'wxaccount_id' => $this -> wxaccount['id']);//wxaccount_id表示所属的微信公众账号
        $user = apiCall(WxuserApi::GET_INFO , array($map));
        $this->assign('d_addr',$user['info']['default_address']);
        $map = array('uid'=>$user['info']['id']);
        $result = apiCall(AddressApi::QUERY_NO_PAGING , array($map));
        $this->assign("addr",$result['info']);
        $this->display();
    }

    /*
    * 选择收货地址2
    * */
    public function select_address_two(){
        $map = array('openid' => $this -> openid, 'wxaccount_id' => $this -> wxaccount['id']);//wxaccount_id表示所属的微信公众账号
        $user = apiCall(WxuserApi::GET_INFO , array($map));
        $this->assign('d_addr',$user['info']['default_address']);
        $map = array('uid'=>$user['info']['id']);
        $result = apiCall(AddressApi::QUERY_NO_PAGING , array($map));
        $this->assign("addr",$result['info']);
        $this->display();
    }

    /*
     *私人定制选择地址
     * */
    public function select_address_suit(){
        $map = array('openid' => $this -> openid, 'wxaccount_id' => $this -> wxaccount['id']);//wxaccount_id表示所属的微信公众账号
        $user = apiCall(WxuserApi::GET_INFO , array($map));
        $this->assign('d_addr',$user['info']['default_address']);
        $map = array('uid'=>$user['info']['id']);
        $result = apiCall(AddressApi::QUERY_NO_PAGING , array($map));
        $this->assign("addr",$result['info']);
        $this->display();
    }
}